package com.vf.cloud.paas.app.master.api.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.vf.cloud.paas.app.master.common.domain.Project;
import com.vf.cloud.paas.app.master.common.domain.User;
import com.vf.cloud.paas.app.master.common.domain.UserProject;
import com.vf.cloud.paas.common.datasource.annotation.Tx;
import com.vf.cloud.paas.common.log.annotation.RLog;
import com.vf.cloud.paas.common.security.annotation.TokenRequired;
import com.vf.cloud.paas.common.util.R;
import com.vf.cloud.paas.common.util.WebUtils;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/user/project")
public class UserProjectController {

	@RLog(value = "获取用户授权底板列表", type = "user")
	@TokenRequired
	@PostMapping(value = { "/list" }, produces = "application/json; charset=utf-8")
	public R<List<User>> list(HttpServletRequest request,@RequestBody UserProject param) throws Exception {
		
		User user = WebUtils.getCurUser(request);
		if (user == null) {
			return R.expired();
		}

		if (StrKit.equals("administrator", user.getUsername())) {
			String _sql="select a.id,a.name,(CASE  WHEN (SELECT COUNT(*) AS COUNT FROM master_user_project WHERE master_id=a.ID and project_id=?)>0 THEN TRUE ELSE  FALSE END ) AS status  from master_user a where a.pid='-1' ";
			if(!StrKit.isBlank(param.getKeywords())) {
				_sql+=" and a.name like '%"+param.getKeywords()+"%'";
			}
			List<User> users=User.dao.find(_sql,param.getProjectId());
			return R.ok(users);
		}else {
			
			String _sql="select a.id,a.name,(CASE  WHEN (SELECT COUNT(*) AS COUNT FROM master_user_project WHERE master_id=a.ID and project_id=?)>0 THEN TRUE ELSE  FALSE END ) AS status  from master_user a where a.pid='"+user.getId()+"' ";
			if(!StrKit.isBlank(param.getKeywords())) {
				_sql+=" and a.name like '%"+param.getKeywords()+"%'";
			}
			List<User> users=User.dao.find(_sql,param.getProjectId());
			return R.ok(users);
		}
		

	}
	
	@RLog(value = "保存用户授权底板", type = "user")
	@TokenRequired
	@Tx
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<UserProject> save(@RequestBody UserProject param) {
		if(StrKit.equals(param.getStatus(), "1")) {
			if(param.save()) {
				return R.ok();
			}
		}
		if(StrKit.equals(param.getStatus(), "0")) {
			if(UserProject.dao.deleteByIds(param.getMasterId(),param.getProjectId())) {
				return R.ok();
			}
		}
		return R.failed();
	}

	@GetMapping(value = { "/page" }, produces = "application/json; charset=utf-8")
	public R<Page<Project>> findList(HttpServletRequest request,
			@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(name = "status", required = false, defaultValue = "") String status,
			@RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
			@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) throws Exception {

		User user = WebUtils.getCurUser(request);
		String sqlExceptSelect = " from master_project a,master_user_project b WHERE a.id=b.project_id and b.user_id='"
				+ user.getPid() + "' ";
		sqlExceptSelect += "  ORDER BY   a.modify_time desc ";
		Page<Project> pages = Project.dao.paginate(cur, limit, "select a.* ", sqlExceptSelect);
		return R.ok(pages);
	}

}
